﻿<MudTable @ref="Table" T="Element" Items="_items" @bind-SelectedItem="_selectedItem"
          RowEditPreview="BackupItem" RowEditCancel="ResetItemToOriginalValues" CanCancelEdit="true">
    <HeaderContent>
        <MudTh>Value</MudTh>
    </HeaderContent>
    <RowTemplate>
        <MudTd>@context.Value</MudTd>
    </RowTemplate>
    <RowEditingTemplate>
        <MudTd>
            <MudTextField id="@context.Id" @bind-Value="@context.Value"></MudTextField>
        </MudTd>
    </RowEditingTemplate>
</MudTable>

@code {
    private readonly Element _elementBeforeEdition = new();

    private Element? _selectedItem;

    private readonly List<Element> _items =
    [
        new() { Value = "A", Id = "Id1" },
        new() { Value = "B", Id = "Id2" },
        new() { Value = "C", Id = "Id3" }
    ];

    public MudTable<Element> Table { get; private set; } = null!;

    private void BackupItem(object element)
    {
        _elementBeforeEdition.Value = ((Element)element).Value;
    }

    private void ResetItemToOriginalValues(object element)
    {
        ((Element)element).Value = _elementBeforeEdition.Value;
    }

    public class Element
    {
        public string? Id { get; set; }

        public string? Value { get; set; }
    }
}